<?php
/*
 * Created on 20.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 define ('MODEL_SUIT_FIND',			'SELECT * FROM `sets`,`goods` WHERE `sets`.`good_id`=`goods`.`id` AND `sets`.`id` = ? ');
 define ('MODEL_SUIT_FIND_ALL',		'SELECT id, name FROM `sets_name`');
 define	('MODEL_SUIT_ADD',			'INSERT INTO `sets_name` (`id` ,`name` ) VALUES (NULL, ? )');
 define ('MODEL_SUIT_ADD_GOOD',		'INSERT INTO `sets` (`id` ,`good_id` ) VALUES (?, ?)');
 define ('MODEL_SUIT_DELETE',		'DELETE FROM `sets` WHERE `id` = ?');
 define ('MODEL_SUIT_DELETE_NAME',	'DELETE FROM `sets_name` WHERE `id` = ?');
 define ('MODEL_SUIT_DELETE_GOOD',	'DELETE FROM `sets` WHERE `id` = ? AND `good_id` = ?');
 define	('MODEL_SUIT_GET_FULL_INFO','SELECT sets.id as `set`, `goods`.*  FROM `sets` , `goods` WHERE `sets`.`good_id` = `goods`.`id`');
 
 class Model_Suit extends Models_Base {
 	
 	private $suit;
 	
 	function __construct( $id= null ) {
 		parent::__construct();
 	
 		$this->good = $this->find(array(id => $id));
 	}
 	
 	function find($args = null) {
 		
 		if (empty($args[from])) 
 			$start = 0;
 
  		if (empty($args[limit])) 
 			$count = 5000;			
 			 		
 		if(isset($args[id])) {
 			return $this->getAll(MODEL_SUIT_FIND,array($args[id]));
 		} else {
 			return $this->getAll(MODEL_SUIT_FIND_ALL,array());
 		}

 	}
 	
 	function add($name) {
 		$this->execute(MODEL_SUIT_ADD,array($name));
 	}
 	
 	function addGood($good_id,$id) {
 		$this->execute(MODEL_SUIT_ADD_GOOD,array($id,$good_id));
 	}
 	
 	function delete($id) {
 		$this->execute(MODEL_SUIT_DELETE,array($id));
 		$this->execute(MODEL_SUIT_DELETE_NAME,array($id));
 	}
 	
 	function deleteGood($id,$good_id) {
 		$this->execute(MODEL_SUIT_DELETE_GOOD,array($id,$good_id));
 	}
 	
 	function details() {

		$sth = $this->dbh->prepare(MODEL_SUIT_GET_FULL_INFO);
		$sth->execute();

 		
 		foreach ($sth->fetchAll() as $row) {
 			$out[$row[set]][] = $row;
 		}
 		
 		return $out;
		
 	}
 	
 }